package com.banmao.algorithm.No401To500.No530;

import java.util.ArrayList;

/**
 *  中序遍历获得递增序列
 */
public class Solution2 {

    private ArrayList<Integer> list = new ArrayList<Integer>();

    public int getMinimumDifference(TreeNode root) {

        recursice(root);
        // 遍历list，因为是递增，所以直接做两个邻数的减法即可
        int minSub = Integer.MAX_VALUE;
        for (int i = 1; i < list.size(); i++) {
            Integer left = list.get(i - 1);
            Integer right = list.get(i);
            if(right - left < minSub) minSub = right - left;
        }
        return minSub;
    }

    private void recursice(TreeNode root) {
        // 中序遍历，获得递增序列
        if(root == null) return;

        recursice(root.left);
        list.add(root.val);
        recursice(root.right);

    }

}
